import { BSON } from "bson";
import { Entity } from "../../../../../Core/Module/Entity/Entity";
import { Object2Bson } from "../../../../../Decorator/Decorator";
import { MongoHelper } from "../../../../../Core/Serialize/MongoHelper";
import { Root } from "../../../../../Core/Module/Entity/Root";

export class BsonHelper {

    public static Deserialize<T extends Entity>(clazz: typeof Entity, u8a: Uint8Array): T {
        let data = BSON.deserialize(u8a);
        let entity = Root.Instance.Scene.AddChild<T>(clazz);
        Root.Instance.Scene.RemoveChild(entity.Id);
        MongoHelper.FromBson(entity, data);
        return entity;
    }

    public static Serialize<T extends Entity>(obj: T): Uint8Array {
        return BSON.serialize(Object2Bson(obj));
    }

}